<?php

	/**
	 * 
	 */
	class SluzobneCesty_model extends CI_Model {
		
		function __construct() {
			parent::__construct();
		}
		
		function add($table,$data){
			return $this->db->insert($table,$data);
		}
		
		function zmen($table,$data){
			return $this->db->where('id',$data['id'])
					 	->update($table, $data);					 
		}
		
		function zmenPids($table, $id, $str){
			return $this->db->where('id', $id)
							->update($table, array('person_id'=>$str)); 
		}
		
		function delete($table,$id){
			return $this->db->delete($table, array('id'=>$id));
		}
		
		function Search($data){
			//TODO
		}
		
		function getOdCestaById($id){
			$q = $this->db->get_where('odcestovani',array('id'=>$id));
			if( $q->num_rows() > 0 ) return $q->result_array();
			else return FALSE;			
		}
		
		function getPriCestaById($id){
			$q = $this->db->get_where('pricestovani',array('id'=>$id));
			if( $q->num_rows() > 0 )return $q->result_array();
			else return FALSE;
		}
		
		function getOdCesty($filter = array('page' => 1, 'per_page' => 20)){
                        $this->db->from('odcestovani');
                        if (isset($filter['city']) && $filter['city'] !== '') {
                            $this->db->where('city', $filter['city']);
                        }
                        if (isset($filter['country']) && $filter['country'] !== '') {
                            $this->db->where('country', $filter['country']);
                        }
                        if (isset($filter['organization']) && $filter['organization'] !== '') {
                            $this->db->where('organization', $filter['organization']);
                        }
                        if (isset($filter['person_id']) && (int)$filter['person_id'] > 0) {
                            $this->db->where('FIND_IN_SET(\'' . (int)$filter['person_id'] . '\', `person_id`)');
                        }
                        if (isset($filter['date']) && preg_match('#^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$#', $filter['date'])) {
                            $this->db->where('date_of_arrival <=', $filter['date']);
                            $this->db->where('date_of_departure >=', $filter['date']);
                        }
                        $pagination_data = $this->paginationPreparation(isset($filter['page']) ? (int)$filter['page'] : 1, isset($filter['per_page']) ? (int)$filter['per_page'] : 20, $this->db);
                        $this->pagination($pagination_data['total_rows'], $pagination_data['total_pages'], $pagination_data['current_page'], isset($filter['per_page']) ? (int)$filter['per_page'] : 20, $this->db);
                        $this->db->order_by('date_of_arrival', 'desc');
                        $q = $this->db->get();
			if( $q->num_rows() > 0 )return $q->result_array();
			else return FALSE;
		}
		
		function getPriCesty($filter = array('page' => 1, 'per_page' => 20)){
                        $this->db->from('pricestovani');
                        if (isset($filter['city']) && $filter['city'] !== '') {
                            $this->db->where('city', $filter['city']);
                        }
                        if (isset($filter['country']) && $filter['country'] !== '') {
                            $this->db->where('country', $filter['country']);
                        }
                        if (isset($filter['organization']) && $filter['organization'] !== '') {
                            $this->db->where('organization', $filter['organization']);
                        }
                        if (isset($filter['person_id']) && (int)$filter['person_id'] > 0) {
                            $this->db->where('FIND_IN_SET(\'' . (int)$filter['person_id'] . '\', `person_id`)');
                        }
                        if (isset($filter['date']) && preg_match('#^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$#', $filter['date'])) {
                            $this->db->where('date_of_arrival <=', $filter['date']);
                            $this->db->where('date_of_departure >=', $filter['date']);
                        }
                        $pagination_data = $this->paginationPreparation(isset($filter['page']) ? (int)$filter['page'] : 1, isset($filter['per_page']) ? (int)$filter['per_page'] : 20, $this->db);
                        $this->pagination($pagination_data['total_rows'], $pagination_data['total_pages'], $pagination_data['current_page'], isset($filter['per_page']) ? (int)$filter['per_page'] : 20, $this->db);
                        $this->db->order_by('date_of_arrival', 'desc');
                        $q = $this->db->get();
			if( $q->num_rows() > 0 )return $q->result_array();
			else return FALSE;
		}
		
		function getCestuById($from, $id){
			$q = $this->db->get_where($from, array('id'=>$id));	
			if( $q->num_rows() > 0 ) return $q->result_array();
			else return FALSE;	
		}
		
		function kontrola($table, $str){
			$result = $this->db->get_where($table, array('unique'=>$str));
							   
			if( $result->num_rows() == 0 ){
				return FALSE;
			} else {
				return TRUE;
			}
		}
		
		function getByUnique($table, $str){
			$result = $this->db->where('unique',$str)
							   ->select('id')
							   ->get($table);
			if ( $result->num_rows() > 0 ){
				return $result->row_array();
			} else {
				return FALSE;
			}
		}
                
                function pagination($total_rows, $total_pages, $page, $per_page, $db_object) {
                    if ($total_rows == 0) { return; }
                    $first = ($page - 1) * $per_page;
                    if ($first > $total_rows) {
                        $first = ($total_pages - 1) * $per_page;
                    }
                    $db_object->limit($per_page, $first);
                }
                
                function paginationPreparation($page, $per_page, $db_object) {
                    $db2 = clone $db_object;
                    $db2->select('COUNT(id) AS total', FALSE);
                    $q2 = $db2->get();
                    $total_rows = $q2->row()->total;
                    $q2->free_result();
                    $total_pages = ceil($total_rows / $per_page);
                    $current_page = $page >= 1 && $page <= $total_pages ? $page : ($page < 1 ? 1 : $total_pages);
                    $result = array('total_rows' => $total_rows, 'total_pages' => $total_pages, 'current_page' => $current_page);
                    $this->load->vars(array('paginationData' => $result));
                    return $result;
                }
	}
	